<include file="Public/header" />
<title>我的桌面</title>
</head>
<body>
<div class="page-container">
    <div class="cl pd-5 bg-1 bk-gray mt-20">
        <span class="l">
            <input type="text" name="name" id="name" placeholder=" 名称" value="<{$Think.get.name}>" style="width:250px" class="input-text">
             <button id="search" class="btn btn-success" type="button"><i class="Hui-iconfont"></i> 搜索</button>
            <a href="javascript:;" onclick="delAll()" class="btn btn-danger radius"><i class="Hui-iconfont"></i> 批量删除</a>
            <a class="btn btn-primary radius" data-title="添加单位" _href="article-add.html" onclick="add()" href="javascript:;"><i class="Hui-iconfont"></i> 添加单位</a>
        </span>
        <span class="r">共有数据：<strong><{$count}></strong> 条</span>
    </div>
    <div class="mt-20">
        <div id="DataTables_Table_0_wrapper" class="dataTables_wrapper no-footer">
            <table class="table table-border table-bordered table-bg table-hover table-sort dataTable no-footer" id="DataTables_Table_0" role="grid" aria-describedby="DataTables_Table_0_info">
                <thead>
                <tr class="text-c" role="row">
                    <th width="25" style="width: 25px;"><input type="checkbox" name="" value=""></th>
                    <th width="80" class="sorting_desc" style="width: 120px;">ID</th>
                    <th tabindex="0" style="width: 487px;">名称(点击名称可以编辑)</th>
                    <th width="120" style="width: 220px;">编码</th>
                    <th width="120" style="width: 220px;">操作</th>
                </tr>
                </thead>
                <tbody>
                <foreach name="list" item="data">
                    <tr class="text-c odd" role="row">
                        <td><input type="checkbox" value="<{$data.id}>" name=""></td>
                        <td class="sorting_1"><{$data.id}></td>
                        <td class="text-l"><span tabindex="<{$data.id}>" onclick="sp_show(this)" style="width: 90%; padding: 4px 12px; "><{$data.unit_name}></span></td>
                        <td><{$data.unit_no}></td>
                        <td>
                            <a style="text-decoration:none" class="ml-5" onclick="del('<{$data.id}>')" href="javascript:;" title="删除"><i class="Hui-iconfont"></i></a>
                        </td>
                    </tr>
                </foreach>
                </tbody>
            </table>
            <div class="page">
                <{$page}>
            </div>
        </div>
    </div>
    <script id="add_text" type="text/html">
        <div style="padding: 12px;">
            <table>
                <tr>
                    <td width="60" height="40">单位名称</td>
                    <td><input type="text" class="input-text" /></td>
                </tr>
                <tr>
                    <td width="60">单位编码</td>
                    <td><input type="text" class="input-text" /></td>
                </tr>
            </table>
        </div>
    </script>
    <include file="Public/footer" />
    <include file="Public/footer_js" />
    <script>
        $("#search").click(function (){
            location.href="__CONTROLLER__/index/name/"+$("#name").val();
        });
        var m=0;//防止blur事件多次触发
        function sp_show(_this){
            var value=$(_this).html();
            $(_this).attr('contenteditable',true);
            $(_this).css({'border':'#ddd solid 1px','padding':'2px 8px','background-color':'#fbb'});
            $(_this).bind('blur',function (){
                if(m!=0){
                    return false;
                }
                $(_this).attr('contenteditable',false);
                $(_this).css({'border':'none','padding':'2px 8px','background-color':'#fff'});
                if(value==$(_this).html()){
                    return false;
                }
                m=1;
                $.post('__MODULE__/Unit/editD',{
                    name:$(_this).html(),
                    id:$(_this).attr('tabindex'),
                },function (msg){
                    layer.msg(msg.data);
                    m=0;
                    if(msg.flag==1){
                        value=$(_this).html();
                    }
                },'json');
            });
        }
        function edit(_this){
            sp_show($(_this).parent().parent().find('span'));
        }
        function add(){
            var index=layer.open({
                type: 1,
                title:'单位添加',
                skin: 'layui-layer-rim', //加上边框
                area: ['240px', '190px'], //宽高
                content: $("#add_text").html(),
                btn:['确定','取消'],
                id:'post_add',
                yes:function (){
                    var name=$("#post_add input:eq(0)").val();
                    var code=$("#post_add input:eq(1)").val();
                    $.post('__MODULE__/Unit/addD',{
                        name:name,
                        code:code,
                    },function (msg){
                        layer.msg(msg.data);
                        if(msg.flag==1){
                            setTimeout(function (){
                                location.reload();
                            },800);
                        }
                    },'json');
                }
            });
        }
        function delAll(){
            var all_ipt=$("#DataTables_Table_0_wrapper input[type=checkbox]:checked");
            var group_id='';
            for(var i=0;i<all_ipt.length;i++){
                if(all_ipt.eq(i).val()!==''){
                    group_id+='@'+all_ipt.eq(i).val();
                }
            }
            if(group_id.substring(1)==''){
                layer.msg('请勾选要删除的信息');
                return false;
            }
            del(group_id.substring(1));
        }
        function del(id){
            $.get('__MODULE__/Unit/del',{
                id:id,
            },function (msg){
                layer.msg(msg.data);
                if(msg.flag==1){
                    setTimeout(function (){
                        location.reload();
                    },800);
                }
            },'json');
        }
    </script>
</body>
</html>